#include "ring_buffer.h"
#include <string.h>

void ring_buffer_init(ring_buffer *cir_buf)
{
    cir_buf->pW = 0;
    cir_buf->pR = 0;
}


int ring_buffer_write(ring_buffer *cir_buf, uint8_t data)
{
    uint32_t i = (cir_buf->pW+1) % BUFFER_SIZE;
    if(i != cir_buf->pR)
    {
        cir_buf->buff[cir_buf->pW] = data;
        cir_buf->pW = i;
        return PDSUCCESS;
    }
    return PDFAIL;
}


int ring_buffer_read(ring_buffer *cir_buf, uint8_t *data)
{
    if(cir_buf->pW == cir_buf->pR)
    {
        return PDFAIL;
    }else{
        *data = cir_buf->buff[cir_buf->pR];
        cir_buf->pR = (cir_buf->pR+1) % BUFFER_SIZE;
        return PDSUCCESS;
    }
}

